/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.start.model;

import business.service.MongoDbFactory;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.star.common.PostData;
import com.star.common.PostOnHomeData;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.log4j.Logger;
import org.bson.types.ObjectId;

/**
 *
 * @author Administrator
 */
public class PostOnHomeModel {
    private static final String POSTONHOME_COLLECTION = "post_on_home";
    private static final Logger logger = Logger.getLogger(PostOnHomeModel.class);
    private static final Lock createLock_ = new ReentrantLock();
    private static PostOnHomeModel _instance = null;
    
    public static PostOnHomeModel getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new PostOnHomeModel();

                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }
    
    
    public PostOnHomeModel() {
        try {
        } catch (Exception ex) {
            logger.error("PostOnHomeModel: " + ex);
        }
    }
    
    
    public List<PostOnHomeData> getPostOnHome(int start, int limit) {
        List<PostOnHomeData> lstPostOnHome = null;
        try {
            BasicDBObject basicDBObject = new BasicDBObject("created", -1);
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(POSTONHOME_COLLECTION);
            DBCursor cursor = coll.find().sort(basicDBObject);

            if (limit != -1) {
                cursor = cursor.skip(start).limit(limit);
            }

            lstPostOnHome = new ArrayList<>();
            while (cursor.hasNext()) {
                DBObject dbObject = cursor.next();
                String objectId = ((ObjectId) dbObject.get("_id")).toString();
                String post1 = (String) dbObject.get("post1");
                String post2 = (String) dbObject.get("post2");
                String post3 = (String) dbObject.get("post3");
                String post4 = (String) dbObject.get("post4");
                String post5 = (String) dbObject.get("post5");
                String post6 = (String) dbObject.get("post6");
                String post7 = (String) dbObject.get("post7");
                String post8 = (String) dbObject.get("post8");
                String post9 = (String) dbObject.get("post9");
                String post10 = (String) dbObject.get("post10");
                String post11 = (String) dbObject.get("post11");
                String post12 = (String) dbObject.get("post12");
                String post13 = (String) dbObject.get("post13");
                String post14 = (String) dbObject.get("post14");    
                String post15 = (String) dbObject.get("post15");
                String post16 = (String) dbObject.get("post16");
                String post17 = (String) dbObject.get("post17");
                String post18 = (String) dbObject.get("post18");
                String post19 = (String) dbObject.get("post19");
                String post20 = (String) dbObject.get("post20");    
                String lstCateLocal = (String) dbObject.get("lstCateLocal");
                String lstCateInter = (String) dbObject.get("lstCateInter");
                String author = (String) dbObject.get("author");
                String editor = (String) dbObject.get("editor");
                long updated = (long) dbObject.get("updated");
                long created = (long) dbObject.get("created");
                
                PostOnHomeData postOnHomeData = new PostOnHomeData(objectId, post1, post2, post3, post4, post5, post6, 
                                                        post7, post8, post9, post10, post11, post12, post13, 
                                                        post14, post15, post16, post17, post18, post19, post20, 
                                                        lstCateLocal, lstCateInter, author, editor, updated, created);
                lstPostOnHome.add(postOnHomeData);
            }
        } catch (Exception ex) {
            logger.error("PostOnHomeModel.getPostOnHome: " + ex.getMessage(), ex);
        }
        return lstPostOnHome;
    }
    
    
    public String addNewPostOnHome(PostOnHomeData postOnHomeData) {
        String id = "";
        try {
            BasicDBObject basicDBObject = new BasicDBObject()    
                    .append("post1", postOnHomeData.getPost1())
                    .append("post2", postOnHomeData.getPost2())
                    .append("post3", postOnHomeData.getPost3())
                    .append("post4", postOnHomeData.getPost4())
                    .append("post5", postOnHomeData.getPost5())
                    .append("post6", postOnHomeData.getPost6())
                    .append("post7", postOnHomeData.getPost7())
                    .append("post8", postOnHomeData.getPost8())
                    .append("post9", postOnHomeData.getPost9())
                    .append("post10", postOnHomeData.getPost10())
                    .append("post11", postOnHomeData.getPost11())
                    .append("post12", postOnHomeData.getPost12())
                    .append("post13", postOnHomeData.getPost13())
                    .append("post14", postOnHomeData.getPost14())
                    .append("post15", postOnHomeData.getPost15())
                    .append("post16", postOnHomeData.getPost16())
                    .append("post17", postOnHomeData.getPost17())
                    .append("post18", postOnHomeData.getPost18())
                    .append("post19", postOnHomeData.getPost19())
                    .append("post20", postOnHomeData.getPost20())
                    .append("lstCateLocal", postOnHomeData.getLstCateLocal())
                    .append("lstCateInter", postOnHomeData.getLstCateInter())      
                    .append("editor", postOnHomeData.getAuthor())
                    .append("author", postOnHomeData.getAuthor())
                    .append("updated", System.currentTimeMillis())
                    .append("created", System.currentTimeMillis());
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(POSTONHOME_COLLECTION);
            coll.insert(basicDBObject);
            
            ObjectId objectId = (ObjectId)basicDBObject.get( "_id" );
            id = objectId.toString();
        } catch (Exception ex) {
            logger.error("PostOnHomeModel.addPostOnHome: " + ex.getMessage(), ex);
        }
        return id;
    }
    
    
    public boolean updatePostOnHome(PostOnHomeData postOnHomeData) {
        try {
            BasicDBObject basicDBObject = new BasicDBObject() 
                    .append("post1", postOnHomeData.getPost1())
                    .append("post2", postOnHomeData.getPost2())
                    .append("post3", postOnHomeData.getPost3())
                    .append("post4", postOnHomeData.getPost4())
                    .append("post5", postOnHomeData.getPost5())
                    .append("post6", postOnHomeData.getPost6())
                    .append("post7", postOnHomeData.getPost7())
                    .append("post8", postOnHomeData.getPost8())
                    .append("post9", postOnHomeData.getPost9())
                    .append("post10", postOnHomeData.getPost10())
                    .append("post11", postOnHomeData.getPost11())
                    .append("post12", postOnHomeData.getPost12())
                    .append("post13", postOnHomeData.getPost13())
                    .append("post14", postOnHomeData.getPost14())
                    .append("post15", postOnHomeData.getPost15())
                    .append("post16", postOnHomeData.getPost16())
                    .append("post17", postOnHomeData.getPost17())
                    .append("post18", postOnHomeData.getPost18())
                    .append("post19", postOnHomeData.getPost19())
                    .append("post20", postOnHomeData.getPost20())
                    .append("lstCateLocal", postOnHomeData.getLstCateLocal())
                    .append("lstCateInter", postOnHomeData.getLstCateInter())      
                    .append("editor", postOnHomeData.getEditor())                    
                    .append("updated", System.currentTimeMillis());   
                    
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(POSTONHOME_COLLECTION);
            
            BasicDBObject updateDBObject = new BasicDBObject("$set", basicDBObject);
            BasicDBObject searchDBQuery = new BasicDBObject("_id", new ObjectId(postOnHomeData.getId()));
            coll.update(searchDBQuery, updateDBObject);
            return true;
        } catch (Exception ex) {
            logger.error("PostOnHomeModel.updatePostOnHome: " + ex.getMessage());
        }
        return false;
    }
}
